from pygame.constants import MOUSEBUTTONDOWN , MOUSEMOTION
from pygame.rect import Rect
from pygame.surface import Surface

from libs.ZDisplay import ZDisplay
from libs.ZText import ZText
from libs.fun import create_image

class ZButton ( ZDisplay ) :
    def __init__ ( self , father , default_src , over_src , rect = None ) :
        super ( ZButton , self ).__init__ ( father )
        if isinstance ( default_src , Surface ) :
            self.default_image = default_src
        else :
            self.default_image = create_image ( default_src )
        if isinstance ( over_src , Surface ) :
            self.over_image = over_src
        else :
            self.over_image = create_image ( over_src )
        self.image = self.default_image
        self.running = True
        self.respose = self.running
        if rect :
            if isinstance ( rect , Rect ) :
                self.rect = rect
            else :
                self.rect = Rect ( rect )
        else :
            self.rect = self.image.get_rect ( )

    def click ( self , m_keys , m_pos ) :
        if self.rect.collidepoint ( m_pos ) :
            if m_keys [ 0 ] :
                self.action ( )
            if m_keys [ 1 ] :
                self.action_mid ( )
            if m_keys [ 2 ] :
                self.action_right ( )

    def isover ( self , m_pos ) :
        return True if self.rect.collidepoint ( m_pos ) else False

    def draw ( self ) :
        if isinstance ( self.image , ZText ) :
            self.father.surface.blit ( self.image.image , self._pos )
        else :
            self.father.surface.blit ( self.image , self._pos )

    def update ( self , event , keys , m_keys , m_pos ) :
        if self.respose :
            if event.type == MOUSEBUTTONDOWN :
                self.click ( m_keys , m_pos )
            if event.type == MOUSEMOTION and self.isover ( m_pos ) :
                self.over ( )
            else :
                self.out ( )

    def out ( self ) :
        self.image = self.default_image

    def over ( self ) :
        self.image = self.over_image

    def action ( self ) :
        pass

    def action_right ( self ) :
        pass

    def action_mid ( self ) :
        pass
